import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/LoginView.vue')
  },
  {
    path: '/order',
    name: 'order',
    component: () => import('../views/OrderList.vue')
  },
  {
    path: '/user',
    name: 'user',
    component: () => import('../views/UserList.vue')
  },
  {
    path: '/',
    name: 'product',
    component: () => import('../views/ProductList.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 前端路由守卫
router.beforeEach((to, from, next) => {
  const user = localStorage.getItem("user");
  if (!user && to.path !== '/login' && to.path!== '/register') {
    return next("/login");
  }
  next();
})


export default router
